import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xwj on 2017/9/1.
 */
public class HeadMotion {

    private static int frameTime = 1000 / 25;
    private static double initialX = 0, initalY = 0;

    //分析出每一帧的位置
    public static List<Point> readFile(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        List<Point> pointList = new ArrayList(250);
        pointList.add(new Point(initialX, initalY));
        try {
            System.out.println("读文件：" + fileName);
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            int lastRecordTime=0;
            double lastRecordX=0,lastRecordY=0;
            int frame=1;
            // 一次读入一行，直到读入null为文件结束
            long startTime = 0;
            while ((tempString = reader.readLine()) != null) {
                //System.out.println("line " + line + ": " + tempString);// 显示行号
                //line++;
                //格式大概是 时间 x坐标 y坐标
                String[] infos = tempString.split(" ");
                int recordTime = Integer.parseInt(infos[0]);
                double recordX=Double.parseDouble(infos[1]);
                double recordY=Double.parseDouble(infos[2]);
                while(recordTime>=frame*frameTime){
                    pointList.add(new Point(Util.clip(lastRecordX+(recordX-lastRecordX)*(frame*frameTime-lastRecordTime)/(recordTime-lastRecordTime)),
                            Util.clip(lastRecordY+(recordY-lastRecordY)*(frame*frameTime-lastRecordTime)/(recordTime-lastRecordTime))  ));
                    frame++;
                }
                lastRecordTime=recordTime;
                lastRecordX=recordX;
                lastRecordY=recordY;
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e1) {
                }
            }
        }

        return pointList;

    }
}
